package com.atguigu.tingshu.album.api;


import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {
	@Autowired
	private TrackInfoService trackInfoService;
	@Autowired
	private VodService vodService;
	@Autowired
	private AlbumInfoService albumInfoService;
	@Autowired
	private TrackInfoMapper trackInfoMapper;

	/**
	 * 上传音频
	 * @param file
	 * @return
	 */
	@Operation(summary = "上传音频")
	@PostMapping("uploadTrack")
	public Result uploadTrack(MultipartFile file){
		Map<String,Object> map = vodService.uploadTrack(file);
		return Result.ok(map);
	}

	/**
	 * 保存声音
	 * @param trackInfoVo
	 * @return
	 */
    @Operation(summary = "保存声音")
	@PostMapping("saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo){
		Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
		trackInfoService.saveTrackInfo(trackInfoVo,userId);
		return Result.ok();
	}

	/**
	 * 分页查询声音列表
	 * @param pageNo
	 * @param pageSize
	 * @param trackInfoQuery
	 * @return
	 */
	@TsLogin
	@Operation(summary = "分页查询声音列表")
	@PostMapping("findUserTrackPage/{pageNo}/{pageSize}")
	public Result findUserTrackPage(@PathVariable Long pageNo,
									@PathVariable Long pageSize,
									@RequestBody TrackInfoQuery trackInfoQuery){
		Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
		trackInfoQuery.setUserId(userId);
		Page<TrackListVo> listVoPage = new Page<>();
		IPage<TrackListVo> iPage =  trackInfoService.findUserTrackPage(listVoPage,trackInfoQuery);
		return Result.ok(iPage);
	}

	/**
	 * 删除声音
	 * @param trackId
	 * @return
	 */
	@Operation(summary = "删除声音")
	@DeleteMapping("removeTrackInfo/{trackId}")
	private Result removeTrackInfo(@PathVariable Long trackId){
		trackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}

	/**
	 * 回显数据
	 * @param trackId
	 * @return
	 */
	@Operation(summary = "回显数据")
	@GetMapping("getTrackInfo/{trackId}")
	private Result getTrackInfo(@PathVariable Long trackId){
		TrackInfo trackInfo = trackInfoService.getTrackInfo(trackId);
		return Result.ok(trackInfo);
	}

    @Operation(summary = "修改声音信息")
	@PutMapping("updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@PathVariable Long trackId,@RequestBody @Validated TrackInfoVo trackInfoVo){
		trackInfoService.updateTrackInfo(trackId,trackInfoVo);
		return Result.ok();
	}
	/**
	 * 根据专辑Id获取声音列表
	 * @param albumId
	 * @param page
	 * @param limit
	 * @return
	 */
	@TsLogin(required = false)
	@Operation(summary = "获取专辑声音分页列表")
	@GetMapping("findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(
			@Parameter(name = "albumId", description = "专辑id", required = true)
			@PathVariable Long albumId,
			@Parameter(name = "page", description = "当前页码", required = true)
			@PathVariable Long page,
			@Parameter(name = "limit", description = "每页记录数", required = true)
			@PathVariable Long limit) {
		//	获取用户Id
		Long userId = AuthContextHolder.getUserId();
		//	构建分页对象
		Page<AlbumTrackListVo> pageParam = new Page<>(page, limit);
		//	调用服务层方法
		IPage<AlbumTrackListVo> pageModel = trackInfoService.findAlbumTrackPage(pageParam, albumId, userId);
		//	返回数据
		return Result.ok(pageModel);
	}

	/**
	 * 获取用户声音分集购买支付列表
	 * @return
	 */
	@TsLogin
	@Operation(summary = "获取用户声音分集购买支付列表")
	@GetMapping("findUserTrackPaidList/{trackId}")
	public Result findUserTrackPaidList(@PathVariable Long trackId){
		Long userId = AuthContextHolder.getUserId();
		List<Map<String,Object>> list = trackInfoService.findUserTrackPaidList(trackId,userId);
		return Result.ok(list);
	}
	/**
	 * 批量获取下单付费声音列表
	 * @param trackId
	 * @param trackCount
	 * @return
	 */
	@TsLogin
	@GetMapping("findPaidTrackInfoList/{trackId}/{trackCount}")
	Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable("trackId") Long trackId, @PathVariable("trackCount") Integer trackCount){
		Long userId = AuthContextHolder.getUserId();
		List<TrackInfo> list = trackInfoService.fingPaidTrackInfoList(trackId,trackCount,userId);
		return Result.ok(list);
	};


}

